package doc

import (
	"errors"
	"gitee.com/dreamwood/jxgo/v1/core"
	"gitee.com/dreamwood/jxgo/v1/mgorm"
	"gitee.com/dreamwood/jxgo/v1/ss"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"time"
)

type DemoTag struct {
	Id       primitive.ObjectID `jx:"id" json:"id" bson:"_id,omitempty"`
	Name     string             `jx:"name" json:"name" bson:"name"`
	CreateAt time.Time          `json:"createAt" bson:"createAt"`
	UpdateAt time.Time          `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time         `json:"deleteAt" bson:"deleteAt"`
}

func NewDemoTag() *DemoTag {
	return &DemoTag{}
}

func (this *DemoTag) DocGetId() primitive.ObjectID {
	return this.Id
}
func (this *DemoTag) DocSetId(id string) {
	this.Id, _ = primitive.ObjectIDFromHex(id)
}
func (this *DemoTag) DocGetName() string {
	return "jxcore_DemoTag"
}
func (this *DemoTag) DocGetFactory() *mgorm.Factory {
	factory := mgorm.NewFactory()
	factory.Db = core.DBMongo
	factory.Doc = this
	return factory
}

func (this *DemoTag) Create() error {
	return this.DocGetFactory().Create(this)
}
func (this *DemoTag) Replace() error {
	return this.DocGetFactory().Replace(this)
}
func (this *DemoTag) Save() error {
	if this.Id.IsZero() {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.DocGetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.DocGetFactory().Replace(this)
	}
}
func (this *DemoTag) Delete() error {
	return this.DocGetFactory().Delete(this)
}
func (this *DemoTag) UnDelete() error {
	return this.DocGetFactory().UnDelete(this)
}
func (this *DemoTag) Destroy() error {
	return this.DocGetFactory().Destroy(this)
}
func (this *DemoTag) Update(obj ss.Obj) error {
	return this.DocGetFactory().Update(this, obj)
}
func (this *DemoTag) Serialize() ([]byte, error) {
	return core.Json().Marshal(this)
}
func (this *DemoTag) Deserialize(data []byte) error {
	return core.Json().Unmarshal(data, this)
}

type DemoTagCrud struct {
	Doc     *DemoTag
	Factory *mgorm.Factory
}

var CrudDemoTag DemoTagCrud

func NewDemoTagCrud() *DemoTagCrud {
	doc := NewDemoTag()
	return &DemoTagCrud{
		Doc:     doc,
		Factory: doc.DocGetFactory(),
	}
}

func (this *DemoTagCrud) FindById(id string) (*DemoTag, error) {
	doc := &DemoTag{}

	err := this.Factory.FindId(doc, id)
	return doc, err
}
func (this *DemoTagCrud) FindOneBy(filter ss.Obj, orders ss.ArrayString) (*DemoTag, error) {
	doc := &DemoTag{}
	err := this.Factory.FindOneBy(doc, &mgorm.Query{
		Filters: filter,
		Order:   orders,
	})
	return doc, err
}
func (this *DemoTagCrud) FindManyBy(filter ss.Obj, orders ss.ArrayString, page int, size int) ([]*DemoTag, error) {
	docs := make([]*DemoTag, 0)
	err := this.Factory.FindManyBy(&docs, &mgorm.Query{
		Filters: filter,
		Order:   orders,
		Page:    page,
		Size:    size,
	})
	return docs, err
}
func (this *DemoTagCrud) UpdateBy(filter, updater ss.Obj) (int64, error) {
	return this.Factory.UpdateManyBy(updater, &mgorm.Query{
		Filters: filter,
	})
}
func (this *DemoTagCrud) DeleteBy(filter ss.Obj) (int64, error) {
	return this.Factory.DeleteManyBy(&mgorm.Query{
		Filters: filter,
	})
}
func (this *DemoTagCrud) DestroyBy(filter ss.Obj, all bool) (int64, error) {
	if !all && len(filter) == 0 {
		return 0, errors.New("NoneLimitedDelete")
	}
	return this.Factory.DestroyManyBy(&mgorm.Query{
		Filters: filter,
	})
}
func (this *DemoTagCrud) Count(obj ss.Obj) (int64, error) {
	return this.Factory.Count(&mgorm.Query{
		Filters: obj,
	})
}
func (this *DemoTagCrud) SumInt(obj ss.Obj, field string) (int64, error) {
	return this.Factory.AggregateSumInt(&mgorm.Query{
		Filters: obj,
	}, field)
}
func (this *DemoTagCrud) SumFloat(obj ss.Obj, field string) (float64, error) {
	return this.Factory.AggregateSumFloat(&mgorm.Query{
		Filters: obj,
	}, field)
}
